package co.edu.usbcali.rebio.presentation.backEndBeans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpServletRequest;


import co.edu.usbcali.rebio.modelo.RebAgenteBiologico;
import co.edu.usbcali.rebio.modelo.RebCliente;
import co.edu.usbcali.rebio.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.rebio.utilities.FacesUtils;
import co.edu.usbcali.rebio.utilities.Utilities;

import com.icesoft.faces.async.render.RenderManager;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;

import com.icesoft.faces.context.effects.JavascriptContext;

public class RebReporteUno {

	public HtmlSelectOneMenu cmbAgente;
	public HtmlInputText txtCliente;
	private boolean btnCerrarH = false;
	private HtmlInputText txtNombreCliente;
	private HtmlOutputText txtHadCodigo1;
	private HtmlCommandButton limpiar;
	private boolean flag = true;
	private HtmlCommandButton btnAceptar;
	private HtmlCommandButton btnLupaH;
	private boolean btnErrorf = false;
	private RenderManager renderManager;
	private List<RebCliente> losClientes = new ArrayList<RebCliente>();
	private List<SelectItem> listAgente = new ArrayList<SelectItem>();
	private HtmlDataTable tablaClientes = new HtmlDataTable();
	private String codigoCli;
	
	
	public String action_abrirC() {
		btnCerrarH = true;
		return "";
	}
    
    public String action_cerrarC() {
		btnCerrarH = false;
		return "";
	}
    
    public String action_cerrar2(){
		btnErrorf = false;
		return "";
	}
    
    public String action_CargarC() throws Exception {
    	String codigoCliente = txtHadCodigo1.getValue().toString();
    	try {
			RebCliente cliente = BusinessDelegatorView.getRebCliente(codigoCliente.toString());
			txtCliente.setValue(cliente.getCliNombre());
			codigoCli = cliente.getCliIdentificacion().toString();
			btnCerrarH = false;
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return "";
    }
    
    public String action_clear() {
    	

        	txtCliente.setValue(null);
        	txtCliente.setDisabled(false);
            cmbAgente.setValue("0");
            cmbAgente.setDisabled(false);
            codigoCli = null;
    
        return "";
    } 
    
    public String action_consultarCliente() {
		try {

			String nombreCliente = txtNombreCliente.getValue().toString().toUpperCase();
			if (nombreCliente == null || nombreCliente == "") {
				losClientes = BusinessDelegatorView.findByCriteriaInRebCliente(null,null,null,true);
			} else {
				Object variables[] = {"cliNombre",true,nombreCliente + '%',"Like"};
				losClientes = BusinessDelegatorView.findByCriteriaInRebCliente(variables, null, null, true);
				//System.out.println("Aqui");
			}
			flag = true;
			renderManager.getOnDemandRenderer("rebHaciendaView")
			.requestRender();

		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}
	
	
	public String action_generarReporte() throws Exception{
		
		try{
			
			FacesContext fc = FacesContext.getCurrentInstance();
			ExternalContext ec = fc.getExternalContext();
			HttpServletRequest sr = ((HttpServletRequest)ec.getRequest());
			String scheme = sr.getScheme();
			String serverName = sr.getServerName();
			int port = sr.getServerPort();
			String contextPath = sr.getContextPath();
			
			String url = scheme+"://"+serverName+":"+port+contextPath;
			
			String agente = cmbAgente.getValue().toString();
			String fac = (String) txtCliente.getValue();
			
			String factura = "";
        	if (fac == null || fac == ""){
        		throw new Exception("El Nombre del Cliente es de car&#225;cter obligatorio");
        		
			}else{
				factura = fac;
			}
			
			if(factura.trim().equals("")){
				throw new Exception("El Nombre del Cliente es de car&#225;cter obligatorio");
			}
			
			if(agente.trim().equals("")  || agente.equals("0")) {
				throw new Exception("El Nombre del agente es de car&#225;cter obligatorio");
			}
			
			String reportParams = "factura="+factura+"&agente="+agente;
			//String reportParams = "nit="+factura+"&p2=567";
			//reportParams +="numCot="+txtNoCotizacion.getValue().toString();
			
			
			String params = 
				  "'"+
				  url+"/servlet/ReporteUno?"+
				  	reportParams +
				  "', " +
				  "'reportWindow', " +
				  "'" +
				  "width=1024" +
				  ",height=768" +
				  ",status=no" +
				  ",toolbar=no" +
				  ",menubar=no" +
				  ",location=no" +
				  ",scrollbars=yes" +
				  "'"; 
			
//			System.out.println("window.open("+params+");");
			
			//llamar al servlet SatReportServlet
			//JavascriptContext.addJavascriptCall(fc,"window.open("+params+");");
			JavascriptContext.addJavascriptCall(fc,"location.href="+params+";");	
			
			
			
			
		}catch (Exception e) {
			
			btnErrorf = true;
			btnAceptar.requestFocus();
			FacesUtils.addErrorMessage(e.getMessage());
			
		}
		
		
		
		return "";
		
	}
	
	public String action_generarReporteXLS(){
		
try{
			
			FacesContext fc = FacesContext.getCurrentInstance();
			ExternalContext ec = fc.getExternalContext();
			HttpServletRequest sr = ((HttpServletRequest)ec.getRequest());
			String scheme = sr.getScheme();
			String serverName = sr.getServerName();
			int port = sr.getServerPort();
			String contextPath = sr.getContextPath();
			
			String url = scheme+"://"+serverName+":"+port+contextPath;
			
			String agente = cmbAgente.getValue().toString();
			String fac = (String) txtCliente.getValue();
			
			String factura = "";
			if (fac == null || fac == ""){
        		throw new Exception("El Nombre del Cliente es de car&#225;cter obligatorio");
        		
			}else{
				factura = fac;
			}
        	
			if(agente.trim().equals("")  || agente.equals("0")) {
				throw new Exception("El Nombre del agente es de car&#225;cter obligatorio");
			}
			
			if(factura.trim().equals("")){
				
				throw new Exception("El Nombre del Cliente es de car&#225;cter obligatorio");
			}
			
			String reportParams = "factura="+factura+"&agente="+agente;
			//String reportParams = "nit="+factura+"&p2=567";
			//reportParams +="numCot="+txtNoCotizacion.getValue().toString();
			
			
			String params = 
				  "'"+
				  url+"/servlet/ReporteUnoXLS?"+
				  	reportParams +
				  "', " +
				  "'reportWindow', " +
				  "'" +
				  "width=1024" +
				  ",height=768" +
				  ",status=no" +
				  ",toolbar=no" +
				  ",menubar=no" +
				  ",location=no" +
				  ",scrollbars=yes" +
				  "'"; 
			
//			System.out.println("window.open("+params+");");
			
			//llamar al servlet SatReportServlet
			//JavascriptContext.addJavascriptCall(fc,"window.open("+params+");");
			JavascriptContext.addJavascriptCall(fc,"location.href="+params+";");	
			
			
			
			
		}catch (Exception e) {
			
			btnErrorf = true;
			btnAceptar.requestFocus();
			FacesUtils.addErrorMessage(e.getMessage());
			
		}
		
		
		
		return "";
		
	}
	
	public HtmlSelectOneMenu getCmbAgente() {
		
		try {
    		List<RebAgenteBiologico> list = BusinessDelegatorView.findByCriteriaInRebAgenteBiologico(null, null, null, true);
    		listAgente.clear();
    		listAgente.add(new SelectItem(0,"Seleccione un valor"));
    		
    		for (RebAgenteBiologico agenteBiologico : list) {
    			listAgente.add(new SelectItem(agenteBiologico.getAgbNombre()));
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return cmbAgente;
	}


	public void setCmbAgente(HtmlSelectOneMenu cmbAgente) {
		this.cmbAgente = cmbAgente;
	}

	public HtmlInputText getTxtCliente() {
		return txtCliente;
	}

	public void setTxtCliente(HtmlInputText txtCliente) {
		this.txtCliente = txtCliente;
	}

	public boolean isBtnCerrarH() {
		return btnCerrarH;
	}

	public void setBtnCerrarH(boolean btnCerrarH) {
		this.btnCerrarH = btnCerrarH;
	}

	public HtmlInputText getTxtNombreCliente() {
		return txtNombreCliente;
	}

	public void setTxtNombreCliente(HtmlInputText txtNombreCliente) {
		this.txtNombreCliente = txtNombreCliente;
	}

	public HtmlOutputText getTxtHadCodigo1() {
		return txtHadCodigo1;
	}

	public void setTxtHadCodigo1(HtmlOutputText txtHadCodigo1) {
		this.txtHadCodigo1 = txtHadCodigo1;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	

	public HtmlCommandButton getBtnAceptar() {
		return btnAceptar;
	}

	public void setBtnAceptar(HtmlCommandButton btnAceptar) {
		this.btnAceptar = btnAceptar;
	}

	public HtmlCommandButton getBtnLupaH() {
		return btnLupaH;
	}

	public void setBtnLupaH(HtmlCommandButton btnLupaH) {
		this.btnLupaH = btnLupaH;
	}
	
	public boolean isBtnErrorf() {
		return btnErrorf;
	}

	public void setBtnErrorf(boolean btnErrorf) {
		this.btnErrorf = btnErrorf;
	}

	public RenderManager getRenderManager() {
		return renderManager;
	}

	public void setRenderManager(RenderManager renderManager) {
		this.renderManager = renderManager;
	}

	public List<RebCliente> getLosClientes() {
		return losClientes;
	}

	public void setLosClientes(List<RebCliente> losClientes) {
		this.losClientes = losClientes;
	}

	public HtmlDataTable getTablaClientes() {
		return tablaClientes;
	}

	public void setTablaClientes(HtmlDataTable tablaClientes) {
		this.tablaClientes = tablaClientes;
	}
	
	public List<SelectItem> getListAgente() {
		return listAgente;
	}

	public void setListAgente(List<SelectItem> listAgente) {
		this.listAgente = listAgente;
	}

	public String getCodigoCli() {
		return codigoCli;
	}

	public void setCodigoCli(String codigoCli) {
		this.codigoCli = codigoCli;
	}

	public HtmlCommandButton getLimpiar() {
		return limpiar;
	}

	public void setLimpiar(HtmlCommandButton limpiar) {
		this.limpiar = limpiar;
	}
	
	
	

	

	
	
	
	
}
